/**
 * @brief	
 */
 
/* Headers for drivers */
#include "gpio.h"
#include "common.h"
#include "uart.h"
#include "spi.h"

/* Headers for w5500 */
#include "w5500_conf.h"
#include "w5500.h"

/* Header for internet Protocol */
#include "dhcp.h"
#include "udp_demo.h"
#include "tcp_demo.h"
#include "dns.h"
#include "smtp.h"
//#include "http_server.h"
//#include "httputil.h"
#include "http_client.h"
#include "ping.h"

uint8_t reboot_flag = 0;
extern char getStatus[];

int main(void)
{
	DelayInit();
	/* Basic modules initialize */
	GPIO_QuickInit(HW_GPIOE,  6, kGPIO_Mode_OPP);
	GPIO_QuickInit(HW_GPIOE, 12, kGPIO_Mode_OPP);
	UART_QuickInit(UART0_RX_PD06_TX_PD07, 115200);
	printf("This is an IoT item using W5500\r\n");
	
	/* SPI initialization for W5500 */
    SPI_QuickInit(SPI2_SCK_PB21_SOUT_PB22_SIN_PB23, kSPI_CPOL0_CPHA0, 20*1000*1000);
	/* SPI2_PCS0 */
    GPIO_QuickInit(HW_GPIOB, 20, kGPIO_Mode_OPP);
	/* W5500_RESET */
    GPIO_QuickInit(HW_GPIOB,  0, kGPIO_Mode_OPP);
		
	/* Reset w5500 */
	reset_w5500();
	/* Set nic mac address */
	set_w5500_mac();
//	/* Set nic static ip address */
	set_w5500_ip();
	/* Init recv\send buffers */
	socket_buf_init(txsize, rxsize);
	
	printf("All configrations has been finished!\r\n");
//	printf("%s\r\n", getStatus);
	/* Get ip from DHCP Server */
//	while(dhcp_ok != 1)
//		do_dhcp();
	/* Ping test */
	do_ping();		
	
	while(1)
    {
		/* Polling */
		GPIO_ToggleBit(HW_GPIOE, 6);
        DelayMs(500);
		/* Use W5500 as server */	
//		do_tcp_server();  
		/* Use W5500 as client */
//		do_tcp_client();
		/* UDP */
//		do_udp();
		/* DNS */
//		do_dns();
//		do_dns();
//		printf("A\r\n");
//		do_smtp(); 
//		if(mail_send_ok)
//		while(1);
//		do_https();
		do_http_client();
	}
}

